<?php
namespace Dry\Base;

class Route {

    private $data = array();

    private $enable = array();

    private $disable = array();

    private $get = array();

    private $post = array();

    private $routeByHost = array();

    private $hostDefault = '';

    private $vendorDefault = '';

    private $dirDefault = '';

    public function host($host)
    {
        $this->data['host'] = $host;
        return $this;
    }

    public function hostDefault($host)
    {
        $this->hostDefault = $host;
        return $this;
    }

    public function vendor($vendor)
    {
        $this->data['vendor'] = $vendor;
        return $this;
    }

    public function vendorDefault($vendor)
    {
        $this->vendorDefault = $vendor;
        return $this;
    }

    public function dir($dir)
    {
        $this->data['dir'] = $dir;
        return $this;
    }

    public function dirDefault($dir)
    {
        $this->dirDefault = $dir;
        return $this;
    }

    public function controller($controller)
    {
        $this->data['controller'] = $controller;
        $this->data['controllerFull'] = $controller.'Controller';
        return $this;
    }

    public function action($action)
    {
        $this->data['action'] = $action;
        return $this;
    }

    public function run($run)
    {
        $temp = explode('@', $run);
        $this->controller($temp[0]);
        $this->action($temp[1]);
        return $this;
    }

    public function method($method = 'get,post')
    {
        $this->data['method'] = $method;
        return $this;
    }

    public function path($path)
    {
        $this->data['path'] = $path;
        return $this;
    }

    public function name($name = '*')
    {
        $this->data['name'] = $name;
        return $this;
    }

    public function enable($enable = '1')
    {
        $this->data['enable'] = $enable;
        return $this;
    }

    private function check()
    {
        if(!isset($this->data['host'])){
            $this->host($this->hostDefault);
        }
        if(!isset($this->data['vendor'])){
            $this->vendor($this->vendorDefault);
        }
        if(!isset($this->data['dir'])){
            $this->dir($this->dirDefault);
        }
        if(!isset($this->data['method'])){
            $this->method();
        }
        if(!isset($this->data['name'])){
            $this->name();
        }
        if(!isset($this->data['enable'])){
            $this->enable();
        }
    }

    private function category()
    {
        $method = explode(',', $this->data['method']);
        if(in_array('get', $method)){
            $this->get[] = $this->data;
        }
        if(in_array('post', $method)){
            $this->post[] = $this->data;
        }
        $hostList = explode(',', $this->data['host']);
        foreach($hostList as $host){
            if(in_array('get', $method)){
                $this->routeByHost[$host]['get'][] = $this->data;
            }
            if(in_array('post', $method)){
                $this->routeByHost[$host]['post'][] = $this->data;
            }
        }
    }

    public function ok()
    {
        $this->check();
        ksort($this->data);
        if($this->data['enable']=='0'){
            $this->disable[] = $this->data;
        }
        else{
            $this->enable[] = $this->data;
            $this->category();
        }
        $this->data = array();
        return $this;
    }

    public function getEnable()
    {
        return $this->enable;
    }

    public function getDisable()
    {
        return $this->disable;
    }

    public function getGet()
    {
        return $this->get;
    }

    public function getPost()
    {
        return $this->post;
    }

    public function getRouteByHost($host = '')
    {
        if($host==''){
            return $this->routeByHost;
        }
        else{
            if(isset($this->routeByHost[$host])){
                return $this->routeByHost[$host];
            }
            else{
                return array();
            }
        }
    }

    public function parseSlug($url)
    {
        preg_match_all('/\{[a-zA-Z0-9_]+\}/', $url, $match);
        return $match[0];
    }

    public function find($host, $url, $method = 'get')
    {
        $result = array();
        $routeByHost = $this->getRouteByHost($host);
        $list = $routeByHost[$method];
        foreach($list as $data){
            $path = $data['path'];
            if($path==$url){
                $result = $data;
                break;
            }
            else{
                $slugs = $this->parseSlug($path);
                if(!empty($slugs)){
                    $slugList = array();
                    $reg = str_replace('/', '\/', $path);
                    foreach($slugs as $slug){
                        $temp = str_replace('{', '', $slug);
                        $temp = str_replace('}', '', $temp);
                        $slugList[$temp] = '';
                        $reg = str_replace($slug, "(?P<{$temp}>[a-zA-Z0-9_]+)", $reg);
                    }
                    $reg = "/^{$reg}$/";
                    preg_match($reg, $url, $match);
                    if(!empty($match)){
                        foreach($slugList as $k => $v){
                            $slugList[$k] = $match[$k];
                        }
                        $data['slug'] = $slugList;
                        $result = $data;
                        break;
                    }
                }
            }
        }
        return $result;
    }

}
?>
